package com.jml.bean.属性注册编辑器;

import org.springframework.beans.PropertyEditorRegistrar;
import org.springframework.beans.PropertyEditorRegistry;
import org.springframework.beans.propertyeditors.CustomDateEditor;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *注册Spring自带的属性编辑器CustomDateEditor
 *
 * 首次实例化操作是后置处理器处理中放在AbstractBeanFactory的customEditors中，
 * 然后实例其他实体类的时候初始化BeanWrapperImpl的时候，会把这个放在从factory中有放到对应的BeanWrapperImpl中
 * 就是PropertyEditorRegistrySupport类的customEditors中
 */
public class MyDatePropertyEditorRegistrar implements PropertyEditorRegistrar {
	public MyDatePropertyEditorRegistrar() {
		System.out.println("MyDatePropertyEditorRegistrar初始化开始");
	}

	@Override
	public void registerCustomEditors(PropertyEditorRegistry registry) {
		//PropertyEditorRegistry是被PropertyEditorRegistrySupport实现的
		//第一个参数是key第二个参数是value，放在PropertyEditorRegistrySupport类的customEditors中
		//PropertyEditorRegistrySupport类默认是BeanWrapperImpl继承的
		//CustomDateEditor实现PropertyEditorSupport，赋值的时候也是调用setAsText
		registry.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd") , true));
	}
}
